﻿using AntdUI;
using System.Drawing.Design;
using System.Windows.Forms;
using Button = AntdUI.Button;

namespace WenAntdUI.Controls
{
    [ToolboxItem(true)]
    public class PicFileSelect : IControl
    {
        public PicFileSelect()
        {
            this.Padding = new Padding(5);
            input = new Input()
            {
                Dock = DockStyle.Fill,
                JoinRight = true,
                JoinLeft = true,
                BorderWidth = 1,
            };

            this.Controls.Add(input);

            imgbox = new Imgbox()
            {
                Dock = DockStyle.Left,
                Width = 40,
                BackColor = Color.Transparent,
                Padding = new Padding(5),
            };

            this.Controls.Add(imgbox);


            button = new Button()
            {
                IconSvg = "SelectOutlined",
                BorderWidth = 1,
                Width = 60,
                Dock = DockStyle.Right,
                Text = "选择",
                JoinLeft = true,
            };
            this.Controls.Add(button);

            button.Click += Button_Click;
            input.TextChanged += Input_TextChanged;

        }

        private void Input_TextChanged(object? sender, EventArgs e)
        {
            if (File.Exists(input.Text))
            {
                try
                {
                    var image = Image.FromFile(input.Text);
                    this.Image = image;
                }
                catch (Exception ex)
                {
                    this.Image = null;
                }
            }
        }

        private void Button_Click(object? sender, EventArgs e)
        {
            var dia = new OpenFileDialog()
            {
                Filter = "图片|*.png;*.jpg;*.jpeg|位图|*.bmp|图标|*.ico"
            };

            if (dia.ShowDialog() == DialogResult.OK)
            {
                var filename = dia.FileName;
                var iamge = Image.FromFile(filename);
                this.input.Text = filename;
                this.Image = iamge;
            }
        }

        #region 私有属性
        private readonly Imgbox imgbox;
        private readonly Input input;
        private readonly Button button;
        #endregion

        #region 公有属性
        internal Color? fore;
        /// <summary>
        /// 文字颜色
        /// </summary>
        [Description("文字颜色"), Category("外观"), DefaultValue(null)]
        [Editor(typeof(AntdUI.Design.ColorEditor), typeof(UITypeEditor))]
        public new Color? ForeColor
        {
            get => fore;
            set
            {
                if (fore == value) return;
                fore = value;
                Invalidate();
                OnPropertyChanged(nameof(ForeColor));
            }
        }

        #region 背景
        /// <summary>
        /// 边框颜色
        /// </summary>
        internal Color? borderColor;
        [Description("边框颜色"), Category("边框"), DefaultValue(null)]
        [Editor(typeof(AntdUI.Design.ColorEditor), typeof(UITypeEditor))]
        public Color? BorderColor
        {
            get => borderColor;
            set
            {
                if (borderColor == value) return;
                borderColor = value;
                Invalidate();
                OnPropertyChanged(nameof(BorderColor));
            }
        }

        Color? back;
        /// <summary>
        /// 背景颜色
        /// </summary>
        [Description("背景颜色"), Category("外观"), DefaultValue(null)]
        [Editor(typeof(AntdUI.Design.ColorEditor), typeof(UITypeEditor))]
        public new Color? BackColor
        {
            get => back;
            set
            {
                if (back == value) return;
                back = value;
                Invalidate();
                OnPropertyChanged(nameof(BackColor));
            }
        }

        /// <summary>
        /// 图档显示位置占宽
        /// </summary>
        [Category("WEN"), Description("图档显示位置占宽")]
        public int ImageWidth { get => imgbox.Width; set => imgbox.Width = value; }
        /// <summary>
        /// 显示图片内容
        /// </summary>
        [Category("WEN"), Description("显示图片内容")]
        public Image? Image
        {
            get => imgbox.Image;
            set
            {
                if (imgbox.Image != value && value != null)
                {
                    imgbox.Image = value;
                    ImageChanged?.Invoke(this, value);
                    this.OnPropertyChanged(nameof(Image));
                    this.Invalidate();
                }
            }
        }
        public event EventHandler<Image> ImageChanged;
        #endregion

        #endregion

        #region 重绘

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var g = e.Graphics.High();

            Color _back = BackColor ?? Colour.BgContainer.Get("Input"),
                _fore = ForeColor ?? Colour.Text.Get("Input"),
                _border = borderColor ?? Colour.BorderColor.Get("Input");


            var rect = ClientRectangle.PaddingRect(this.Padding);
            var radius = 5 * Config.Dpi;
            var path = rect.RoundPath(radius);
            using var pen = new Pen(_border, 1);
            using var b = new SolidBrush(_back);
            g.Draw(pen, path);
            g.Fill(b, path);
        }

        #endregion
    }
}
